package com.gsn.common;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Align;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.GsnImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.gdx.GsnParticleActor;
import com.gsn.engine.myplay.GsnLayer;
import com.gsn.poker.asset.FontTexture;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class PortalChangeGoldLayer extends GsnLayer implements ClickListener {
	private static final String tag = "DialogLayer";
	float time = 0.5f;
	protected ParticleEffect clickEff;
	public int id;
	Label labelGold;
	Label labelXu;
	GsnImageButton agree;
	GsnImageButton cancel;

	public PortalChangeGoldLayer(float width, float height) {
		super(width, height);

		PortalTexture portalTexture = PortalTexture.inst();

		Image bgDark = new Image(PortalTexture.inst().bgDarkDialog);
		PortalTexture.inst().scaleActor(bgDark);
		addActor(bgDark);
		bgDark.width = width;
		bgDark.height = height;

		Image bg = new Image(PortalTexture.inst().bgDialog);
		PortalTexture.inst().scaleActor(bg);
		addActor(bg);
		bg.x = (width - bg.width) / 2;
		bg.y = (height - bg.height) / 2;
		float pad = width * 0.01f;
		Label labelTitle = new Label("Thông báo", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelTitle);
		labelTitle.x = bg.x + bg.width * 0.05f;
		labelTitle.y = bg.y + bg.height * 0.92f;

		Label labelContent = new Label("Bạn đồng ý đổi", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelContent);
		labelContent.x = bg.x + (bg.width - labelContent.width) * 0.5f;
		labelContent.y = bg.y + bg.height * 0.6f;

		labelXu = new Label("5.000", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		labelXu.width = bg.width * 0.1f;
		addActor(labelXu);
		labelXu.setAlignment(Align.CENTER);
		labelXu.x = bg.x + bg.width * 0.2f;
		labelXu.y = bg.y + bg.height * 0.5f;

		Image xu = new Image(PortalTexture.inst().iconXuBig);
		PortalTexture.inst().scaleActor(xu);
		addActor(xu);
		xu.x = labelXu.x + labelXu.width + pad;
		xu.y = labelXu.y + labelXu.height * 0.1f;

		Label labelContent1 = new Label("thành", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelContent1);
		labelContent1.x = xu.x + xu.width + pad;
		labelContent1.y = xu.y;

		labelGold = new Label("5.000.000.000", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelGold);
		labelGold.width = bg.width * 0.3f;
		labelGold.setAlignment(Align.CENTER);
		labelGold.x = labelContent1.x + labelContent1.width + pad;
		labelGold.y = xu.y;

		Image gold = new Image(PortalTexture.inst().iconGoldBig);
		PortalTexture.inst().scaleActor(gold);
		addActor(gold);
		gold.x = labelGold.x + labelGold.width + pad;
		gold.y = xu.y + labelGold.height * 0.1f;

		agree = new GsnImageButton(PortalTexture.inst().agreeButton, PortalTexture.inst().agreeButtonPress);
		PortalTexture.inst().scaleActor(agree);
		addActor(agree);
		agree.x = bg.x + bg.width * 0.2f;
		agree.y = bg.y + bg.height * 0.1f;
		agree.setClickListener(this);

		cancel = new GsnImageButton(PortalTexture.inst().cancelButton, PortalTexture.inst().cancelButtonPress);
		PortalTexture.inst().scaleActor(cancel);
		addActor(cancel);
		cancel.x = bg.x + bg.width * 0.8f - cancel.getRealWidth();
		cancel.y = bg.y + bg.height * 0.1f;
		cancel.setClickListener(this);

		clickEff = PortalTexture.inst().clickEff;
	}

	@Override
	public void click(Actor actor, float x, float y) {
		// TODO Auto-generated method stub
		hide();
		if (clickEff != null) {
			Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);
			GsnParticleActor par = new GsnParticleActor(clickEff);
			par.setPostition(v.x, v.y);
			par.start();
			this.addActor(par);
		}

		PokerSound.playClick();
		if (actor == agree) {
			MyPoker.client.send(PacketFactory.shopGold(id));
		} else if (actor == cancel) {

		}

	}

	public InputProcessor oldInput;

	public void show(long xu, long gold, int id) {
		oldInput = Gdx.input.getInputProcessor();
		labelGold.setText(ActorUtility.formatNumber(gold));
		labelXu.setText(ActorUtility.formatNumber(xu));
		visible = true;
		setInputListener();
	}

	public void hide() {
		Gdx.input.setInputProcessor(oldInput);
		oldInput = null;
		visible = false;
	}
	
	@Override
	public boolean keyDown(int keycode) {
		switch (keycode){
		case Keys.A:
			for (GsnLayer l : MyPoker.game.chooseChannelScreen.layers){
				System.out.println(l.getClass().getName());
			}
			break;
		}
		return super.keyDown(keycode);
	}

}
